package com.tinem.platform.service.message.observable;

import com.tinem.platform.client.link.LinkClient;
import com.tinem.platform.module.pojo.enums.web.LinkType;
import com.tinem.platform.module.pojo.enums.message.ResourceType;
import com.tinem.platform.module.pojo.enums.message.PushChannel;
import com.tinem.platform.module.pojo.vo.link.AddReq;
import com.tinem.platform.module.pojo.vo.link.AddRes;
import com.tinem.platform.service.message.config.MailConfiguration;
import cn.hutool.core.util.StrUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className EMailListener
 * @description TODO
 * @site 
 * @company 
 * @create 2020-09-28 17:55
 */
@Service
@Order(10)
public class HtmlListener implements ApplicationListener<MessageObservable> {

    @Autowired
    LinkClient linkClient;

    @Override
    public void onApplicationEvent(MessageObservable event) {
        if(! (event.getSource().getPushChannel().equals(PushChannel.EMAIL)
            || event.getSource().getPushChannel().equals(PushChannel.ALI_EMAIL)
        )){
            return;
        }
        if(!(event.getBody() instanceof String)){
            return;
        }
        String sendBody = (String) event.getBody();
        Document document = Jsoup.parse(sendBody);
        //邮件连接点击事件监听
        if(event.getSource().getExtMailClickLinkEventMonitor() == 1){
            document.select("a").forEach(a->{
                String href = a.attr("href");
                if(StrUtil.isEmpty(href)){
                    return;
                }
                AddRes newLink = linkClient.add(new AddReq(href, LinkType.URL, ResourceType.MESSAGE, event.getId(),null,null,null,null));
                a.attr("href",newLink.getNewLink());
                event.getLinks().add(newLink);
            });
        }

        //邮件图片打开事件监听
        if(event.getSource().getExtMailImageLinkEventMonitor() == 1){
            document.select("img").forEach(img->{
                String href = img.attr("src");
                if(StrUtil.isEmpty(href)){
                    return;
                }
                AddRes newLink = linkClient.add(new AddReq(href, LinkType.URL, ResourceType.MESSAGE, event.getId(),null,null,null,null));
                img.attr("src",newLink.getNewLink());
                event.getLinks().add(newLink);
            });
        }

        //邮件打开事件监听
        if(event.getSource().getExtMailOpenEventMonitor() == 1){
            Elements elements = document.select("img[platform-type="+LinkType.OPEN.name()+"]");
            if(elements.size() == 0){
                // 如果不存在标志打开图片，则插入默认图片
                AddRes openLink = linkClient.add(new AddReq("", LinkType.OPEN,ResourceType.MESSAGE, event.getId(),null,null,null,null));
                event.getLinks().add(openLink);
                document.select("body").first().appendElement("img")
                        .attr("src",openLink.getNewLink())
                        .attr("width","1px")
                        .attr("height","1px");
            }else{
                String oldLink = elements.attr("src");
                AddRes openLink = linkClient.add(new AddReq(oldLink, LinkType.OPEN,ResourceType.MESSAGE, event.getId(),null,null,null,null));
                elements.attr("src",openLink.getNewLink());
                event.getLinks().add(openLink);
            }
        }

        event.setBody(document.body().html());
    }
}
